package com.hao.order.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.common.code.ErrorCode;
import com.hao.common.execption.BusinessException;
import com.hao.common.response.BaseResponse;
import com.hao.common.utils.ResultUtils;
import com.hao.order.app.annotation.LoginCheck;
import com.hao.order.app.model.dto.*;
import com.hao.order.app.model.entity.Order;
import com.hao.order.app.model.vo.OrderVO;
import com.hao.order.app.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

/**
 * @author haoge
 * @version 1.0
 * @date 2023/5/23 18:57
 */
@RestController
@RequestMapping ("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @PostMapping ("/create")
    @LoginCheck
    public BaseResponse<Boolean> createOrder(@RequestBody @Valid CreateOrderRequest createOrderRequest) {
        if (createOrderRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = orderService.createOrder(createOrderRequest);

        return ResultUtils.success(result);
    }

    @GetMapping ("/current/amount")
    public BaseResponse<Double> getCurrentAmount(@RequestParam String orderIds) {
        if (StringUtils.isBlank(orderIds)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        double amount = orderService.getCurrentAmount(orderIds);
        return ResultUtils.success(amount);
    }

    @PostMapping ("/pay")
    public BaseResponse<Boolean> pay(@RequestBody @Valid PayOrderRequest payOrderRequest) {
        if (payOrderRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = orderService.pay(payOrderRequest);
        return ResultUtils.success(result);
    }

    @GetMapping ("/getOrderByUser")
    @LoginCheck
    public BaseResponse<Page<OrderVO>> getOrderByUser(@Valid QueryUserOrderRequest queryUserOrderRequest) {
        if (queryUserOrderRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<OrderVO> orderVOPage = orderService.getOrderByUser(queryUserOrderRequest);
        return ResultUtils.success(orderVOPage);
    }

    @PostMapping ("/delete")
    public BaseResponse<Boolean> deleteOrderById(Long id, Integer count) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (count < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = orderService.deleteOrder(id, count);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "删除失败");
        }
        return ResultUtils.success(result);
    }

    @PostMapping ("/update")
    public BaseResponse<Boolean> updateOrderById(@RequestBody @Valid UpdateOrderRequest updateOrderRequest) {
        if (updateOrderRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = orderService.updateOrderById(updateOrderRequest);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(result);
    }

    @PostMapping ("/list")
    public BaseResponse<Page<Order>> listOrderByPage(ListOrderRequest listOrderRequest) {
        if (listOrderRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Order> orderPage = null;
        try {
            orderPage = orderService.listOrderByPage(listOrderRequest);
        } catch (ParseException e) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }

        return ResultUtils.success(orderPage);
    }
}
